<?php
session_start();

if(isset($_SESSION['permisson']))
     header('Location: index.php');

if(isset($_POST['submit']))
{
   //Truy van co so du lieu
    $username = $_POST['user'];
    $password = $_POST['pass']; 
    //$password = md5($password);
    if ($username && $password) {
        $connect = mysql_connect("localhost", "root","") or die("connect to mysql failed");
        mysql_select_db("tripviet") or die("The database isn't exist");
        
        $query = " SELECT fullname,permission,password
                   FROM account                  
                   WHERE username= '$username'";   
        
        $Result = mysql_query($query);
        $numrow = mysql_num_rows($Result);
        if ($numrow != 0) {
            //Code to login
            while ($row = mysql_fetch_assoc($Result)) {                
                $dbpassword = $row['password'];                 
                $permission = $row['permission'];
            }
            if ($dbpassword == $password) {                
                $_SESSION['login'] = true;
                //Truy van co so du lieu de lay permisson
                $_SESSION['permisson'] = $permission;                
                if($permission == 1)
                {
                    header('Location: index.php');
                }                              
            }
            else
                header('Location: login.php');
            
           }
       }
        
    }
    
?>
<html>
    <head>
        <title>Login</title>
        <link rel="SHORTCUT ICON" href="Manager.ico"/>
        <style type="text/css">
            #left-login{
                width: 200px;                
                color:blue;
                display: inline-block;
            }
            #left-login a{
               text-decoration: none;
               font-weight: bold;
            }
            #left-login img{
                margin-top: 10px;
                margin-left: 20px;
            }
            #parent-login{
                margin: 20px 20px 20px 20px;
                width: 530px;
                height: 350px;
                border:solid 3px blue;
                -moz-border-radius : 5px;
                -webkit-border-radius:5px;
            } 
            
            #parent-login form{
                width: 300px;
                clear: both;
                float: right;
                margin-right: 10px;
                margin-top: 20px; 
                background-color: darkgrey;
            }
        </style>
    </head>
    
    <body>  
        <div id="parent-login">
            <div style="padding-left: 20px;font-weight: bold;font-size: 25px;color: blue;">Login to Administrator control panel</div>
            <br />
            <div id="left-login">
            <p>Use a valid username and password to gain access to the administrator backend</p></br>
            <a href="../index.php">Go to Homepage</a></br>
            <img src="../images/manager/Lock.png" height="150" width="150" />
            </div>
            <form action="" method="post">
                <div style=" border: solid 1px blue;-moz-border-radius : 5px;-webkit-border-radius:5px;">
                    <div style="padding-left: 40px;padding-top: 25px;margin: 10px auto;">
                        Username <input name="user" type="text" />
                    </div>
                    <div style="padding-left: 40px;padding-top: 20px;margin: 10px auto;">
                        Password <input name="pass" type="password" />
                    </div>                             
                    <div style="padding-left: 150px;padding-top: 25px;margin: 10px auto;">
                        <button type="submit" name="submit" style="text-align: justify;" >Login <img src="../images/manager/arrow_login.png" height="16" width="16" /></button>					
                    </div>
                </div>     
            </form>        
        </div>
    </body>
</html>
